package com.jkj.http.sys.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.jkj.http.controller.BaseController;
import com.jkj.http.entity.ZTreeNode;
import com.jkj.http.interceptor.AuthorizeChecked;
import com.jkj.http.sys.service.RoleAuthorizeService;
import com.jkj.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/system/roleAuthorize")
public class RoleAuthorizeController extends BaseController {
    @Autowired
    private RoleAuthorizeService roleAuthorizeService;

    /**
     * 角色授权页面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index() {
        return new ModelAndView("system/roleAuthorize/index");
    }

    /**
     * 根据角色ID获得角色权限关系
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/index", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String index(String roleId) {
        List<ZTreeNode> list = roleAuthorizeService.getZTreeNode(roleId);
        return JsonUtils.toJSON(list);
    }

    /**
     * 给某个角色授权
     *
     * @param session
     * @param roleId
     * @param perIds
     * @return
     */
    @RequestMapping(value = "/form", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String form(HttpSession session, String roleId, String perIds) {
        roleAuthorizeService.authorize(session, roleId, perIds.split(","));
        return success("授权成功");
    }
}
